package org.funtester.performance.books.chapter04.section1;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

/**
 * http构建请求,发送请求,获取相应演示案例
 */
public class HttpDemo {

    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();// 创建默认的httpClient实例
        HttpPost httpPost = new HttpPost();// 创建httppost请求对象
        httpPost.addHeader("Content-Type", "application/json");// 设置请求头内容类型
        httpPost.setEntity(new StringEntity("{\"name\":\"FunTester\"}"));// 设置字符串类型请求体内容
        CloseableHttpResponse response = httpClient.execute(httpPost);// 执行请求并获取响应对象
        response.getStatusLine().getStatusCode();// 获取响应状态码
        response.close();// 关闭响应对象
    }

}
